package com.ys.fmms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ys.fmms.mapper.NodeListMapper;
import com.ys.fmms.pojo.NodeList;
import com.ys.fmms.service.NodeListService;
import com.ys.fmms.util.TimeUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;

@Transactional
@Service(value = "nodeListServiceImpl")
public class NodeListServiceImpl extends ServiceImpl<NodeListMapper, NodeList> implements NodeListService {
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public List<NodeList> getNodeList() {
        QueryWrapper<NodeList> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("id");
        List<NodeList> nodeLists = baseMapper.selectList(wrapper);
        return nodeLists;
    }

    @Override
    public NodeList getOnlineAndTotalAmount() {
        NodeList nodeList = baseMapper.getOnlineNodeAndTotalNode();
        return nodeList;
    }

    @Override
    public List<NodeList> getNewStatusDataList() {
        List<NodeList> nodeLists = baseMapper.getCurrentNewDataStatusList();
        return nodeLists;
    }

    @Override
    public IPage<NodeList> getNodeListByFuzzySearch(Page page, String type, String status) {
        QueryWrapper<NodeList> wrapper = new QueryWrapper<>();
        if (StringUtils.hasText(type)) {
            wrapper.like("type", type);
        } else if (StringUtils.hasText(status)) {
            wrapper.like("status", status);
        }
        wrapper.orderByAsc("id");
        IPage<NodeList> nodeListIPage = baseMapper.selectPage(page, wrapper);
        return nodeListIPage;
    }

    @Override
    public void addOrUpdateNodeListById(String id, NodeList nodeList) {
        NodeList nodeList1 = new NodeList();
        if (StringUtils.hasText(id) && !"0".equals(id)) {
            BeanUtils.copyProperties(nodeList, nodeList1);
            nodeList1.setId(id);
            nodeList1.setUpdateTime(TimeUtils.currentDate());
            baseMapper.updateById(nodeList1);
        } else {
            nodeList.setNodeCreateTime(TimeUtils.currentDate());
            baseMapper.insert(nodeList);
        }
    }

    @Override
    public List<String> getFacilityNameList(String facility) {
        List<String> typeList = null;
        // 存储到Redis里
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.expire(facility, Duration.ofDays(1l));

        if (!redisTemplate.hasKey(facility)) {
            typeList = baseMapper.selectTypeList();
            redisTemplate.opsForList().leftPushAll(facility, typeList);
        } else {
            typeList = redisTemplate.opsForList().range(facility, 0, -1);
        }
        return typeList;
    }
}
